package com.starbaba.core.web.taglib.component;

import java.io.Writer;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.components.Component;

import com.starbaba.core.hibernate.Page;
import com.opensymphony.xwork2.util.ValueStack;


public class PageComponent extends Component {
	
	public static final String DEFAULT_PAGE_NUM_SHOW = "10";
	
	private Page page;
	private String pageNumShow;

	public PageComponent(ValueStack stack) {
		super(stack);
	}

	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer); 
		try {
			if(null == page){
				writer.write("");
				return true;
			}
			String pageStr = "<div class=\"pagination\" totalCount=" + page.getTotalCount() + " numPerPage="+ page.getPageSize() + " pageNumShown=" + getPageNumShow() + " currentPage=" + page.getPageNo() + "></div>";
//			System.out.println("pageStr="+pageStr);
			writer.write(pageStr);
		}catch(Exception ex){
			ex.printStackTrace();
		}
        return result;
    }
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getPageNumShow() {
		try{
			if(StringUtils.isBlank(pageNumShow))return DEFAULT_PAGE_NUM_SHOW;;
			if(Integer.parseInt(pageNumShow.trim()) > 0)return pageNumShow;
		}catch(Exception ex){
		}
		return DEFAULT_PAGE_NUM_SHOW;
	}

	public void setPageNumShow(String pageNumShow) {
		this.pageNumShow = pageNumShow;
	}

}
